package com.william.yun.yunliwuli.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.william.yun.yunliwuli.constant.YunliwuliConstant;
import com.william.yun.yunliwuli.util.HttpUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHeaders;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CoreService {
    public static JSONObject getHttpJsonObject(String token, JSONObject json, String url) throws IOException {
        HashMap<String, String> headers = new HashMap<>();
        headers.put(YunliwuliConstant.AUTHORIZATION_HEADER, token);
        String get = new HttpUtils().get(url, headers);
        return getHttpJsonObject(json, get);
    }

    public static JSONObject postHttpJsonObject(String token, JSONObject object, JSONObject json, String url) throws IOException {
        HashMap<String, String> headers = new HashMap<>();
        headers.put(HttpHeaders.CONTENT_TYPE, YunliwuliConstant.headerJson);
        headers.put(YunliwuliConstant.AUTHORIZATION_HEADER, token);
        String post = new HttpUtils().post(url, object,  headers);
        return getHttpJsonObject(json, post);
    }

    public static JSONObject postHttpJsonObject(String token, JSONArray arry, JSONObject json, String url) throws IOException {
        HashMap<String, String> headers = new HashMap<>();
        headers.put(HttpHeaders.CONTENT_TYPE, YunliwuliConstant.headerJson);
        headers.put(YunliwuliConstant.AUTHORIZATION_HEADER, token);
        String post = new HttpUtils().post(url, arry,  headers);
        return getHttpJsonObject(json, post);
    }

    public static JSONObject putHttpJsonObject(String token,  JSONObject object, JSONObject json, String url) throws IOException {
        HashMap<String, String> headers = new HashMap<>();
        headers.put(HttpHeaders.CONTENT_TYPE, YunliwuliConstant.headerJson);
        headers.put(YunliwuliConstant.AUTHORIZATION_HEADER, token);
        String put = new HttpUtils().put(url, object,  headers);
        return getHttpJsonObject(json, put);
    }

    public static JSONObject deleteHttpJsonObject(String token,  JSONObject json, String url) throws IOException {
        HashMap<String, String> headers = new HashMap<>();
        headers.put(YunliwuliConstant.AUTHORIZATION_HEADER, token);
        headers.put(HttpHeaders.CONTENT_TYPE, YunliwuliConstant.headerJson);
        String delete = new HttpUtils().delete(url, json,  headers);
        return getHttpJsonObject(json, delete);
    }

    private static JSONObject getHttpJsonObject(JSONObject json, String post) {
        if(StringUtils.isNotEmpty(post)){
            return JSONObject.parseObject(post);
        }else{
            json.put("status", "500");
            json.put("msg", "网络错误");
        }
        return json;
    }


    public static JSONObject postJsonResult(JSONObject body, String token, String url) {
        JSONObject json = new JSONObject();
        try {
            JSONObject result = postHttpJsonObject(token, body ,json, url);
            if (result != null) return result;
        }catch (IOException ioException){
            //报错处理
            json.put("status", "500");
            json.put("msg", ioException.getMessage());
        }
        return json;
    }


    public static JSONObject postJsonResult(JSONArray array, String token, String url) {
        JSONObject json = new JSONObject();
        try {
            JSONObject result = postHttpJsonObject(token, array ,json, url);
            if (result != null) return result;
        }catch (IOException ioException){
            //报错处理
            json.put("status", "500");
            json.put("msg", ioException.getMessage());
        }
        return json;
    }

    public  static JSONObject deleteJsonResult(JSONObject body, String token, String url) {
        JSONObject json = new JSONObject();
        try {
            JSONObject result = deleteHttpJsonObject(token, body, url);
            if (result != null) return result;
        }catch (IOException ioException){
            //报错处理
            json.put("status", "500");
            json.put("msg", ioException.getMessage());
        }
        return json;
    }

    public  static JSONObject getJsonResult(JSONObject body, String token, String url) {
        JSONObject json = new JSONObject();
        try {
            JSONObject result = getHttpJsonObject(token ,json, url);
            if (result != null) return result;
        }catch (IOException ioException){
            //报错处理
            json.put("status", "500");
            json.put("msg", ioException.getMessage());
        }
        return json;
    }

    public  static JSONObject putJsonResult(JSONObject body, String token, String url) {
        JSONObject json = new JSONObject();
        try {
            JSONObject result = putHttpJsonObject(token, body ,json, url);
            if (result != null) return result;
        }catch (IOException ioException){
            //报错处理
            json.put("status", "500");
            json.put("msg", ioException.getMessage());
        }
        return json;
    }


    public String jsonToString(JSONObject body, String url){
        Map<String, Object> innerMap = body.getInnerMap();
        List<String> parameter = new ArrayList<>();
        innerMap.forEach((k,v) ->{
            parameter.add(k+"="+ (ObjectUtils.isNotEmpty(v)? v: ""));
        });
        if(parameter.size()>0){
            return url.concat("?").concat(String.join("&", parameter));
        }
        return url;
    }
}
